#include "Mylogger.hh"

Mylogger * Mylogger::_pInstance = nullptr;

Mylogger::Mylogger():_log(Category::getInstance("log")){
    // 1. 准备布局（格式化器）
    PatternLayout * ptnConsole = new PatternLayout();
    ptnConsole->setConversionPattern("%d %c: [%p] %m%n");

    PatternLayout * ptnFile = new PatternLayout();
    ptnFile->setConversionPattern("%d %c: [%p] %m%n");

    PatternLayout * ptnRollingFile = new PatternLayout();
    ptnRollingFile->setConversionPattern("%d %c: [%p] %m%n");

    // 2. 创建输出器与布局绑定 输出器和布局要一对一
    OstreamAppender * pConsole = new OstreamAppender("console", &cout);
    FileAppender * pFile = new FileAppender("file", "res.log");
    RollingFileAppender * pRollingFile = new RollingFileAppender("rollingFile", "rolling.log", 5 * 1024, 9);

    pConsole->setLayout(ptnConsole);
    pFile->setLayout(ptnFile);
    pRollingFile->setLayout(ptnRollingFile);

    // 4.设置优先级（过滤器）和输出器 
    _log.setPriority(Priority::DEBUG);
    _log.setAppender(pConsole);
    _log.setAppender(pFile);
    _log.setAppender(pRollingFile);
}

Mylogger::~Mylogger(){
    Category::shutdown();
}

Mylogger* Mylogger::getInstance(){
    if (_pInstance == nullptr){
        _pInstance = new Mylogger();
    }
    return _pInstance;
}

void Mylogger::destory(){
    if (_pInstance){
        delete _pInstance;
        _pInstance = nullptr;
    }
}

void Mylogger::warn(string msg){_log.warn(msg);}
void Mylogger::error(string msg){_log.error(msg);}
void Mylogger::debug(string msg){_log.debug(msg);}
void Mylogger::info(string msg){_log.info(msg);}
void Mylogger::fatal(string msg){_log.fatal(msg);}
void Mylogger::crit(string msg){_log.crit(msg);}